package com.gonsin.i18n.logic.services;

//import io.swagger.annotations.ApiModel;
//import io.swagger.annotations.ApiModelProperty;
import com.google.gson.GsonBuilder;
import lombok.SneakyThrows;

import java.io.File;
import java.util.HashMap;
import java.util.Map;
import java.util.Timer;
import java.util.concurrent.ConcurrentHashMap;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.gonsin.i18n.logic.config.I18nConfig;
import org.springframework.beans.BeanUtils;

public class KeywordFile {

    // @ApiModelProperty("标记该json文件是否为I18n的文件")
    private String marker;

    // 语言标题
    private String title;

    // 关键字
    private String lang;

    /**
     * 项目名字
     */
    private String projectName;

    // @ApiModelProperty("字符串键值对")
    private Map<String, String> messages;

    private Map<String, Extra> extras;


    public KeywordFile() {
        this.marker = "gonsin";
        this.messages = new ConcurrentHashMap<>();
        this.extras = new ConcurrentHashMap<>();
    }

    public KeywordFile(KeywordFileModel model) {
        this.marker = model.getMarker();
        this.messages = new ConcurrentHashMap<>();
        this.messages.putAll(model.getMessages());
        this.projectName = model.getProjectName();
        this.lang = model.getLang();
        this.title = model.getTitle();
        this.extras = model.getExtras();
        if(extras == null){
            extras = new ConcurrentHashMap<>();
        }
//        BeanUtils.copyProperties(model, this);
    }


    public String getMarker() {
        return marker;
    }

    public void setMarker(String marker) {
        this.marker = marker;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getLang() {
        return lang;
    }

    public void setLang(String lang) {
        this.lang = lang;
    }

    public Map<String, String> getMessages() {
        return messages;
    }

    public void setMessages(Map<String, String> messages) {
        this.messages = messages;
    }

    // 以下为逻辑部分
    private Timer futureTimer;
    private volatile boolean saving = false;

//    /**
//     * 延迟保存
//     * 主要是考虑可能短时间内会有多次保存，，不需要每次保存都产生一次文件写操作。
//     */
//    public void saveInFuture() {
//        if (saving) {
//            return;
//        }
//        synchronized (this) {
//            if (saving) {
//                return;
//            }
//            saving = true;
//        }
//
//        futureTimer = new Timer();
//        futureTimer.schedule(new TimerTask() {
//            @Override
//            public void run() {
//                save();
//                synchronized (KeywordFile.this) {
//                    saving = false;
//                }
//            }
//        }, I18nInstance.CONFIG.getSaveDelay());
//    }

    /**
     * 将keywords持久化到文件
     */
    @SneakyThrows
    @JsonIgnore
    public  void save() {

        synchronized (KeywordFile.class){
            KeywordFileModel model = new KeywordFileModel();
            BeanUtils.copyProperties(this, model);
            model.setMessages(new HashMap<>(this.getMessages()));
            model.setExtras(new HashMap<>(this.extras));
            I18nConfig config = I18nInstance.CONFIG;
            File langFile = new File(config.getLangPath() + File.separator + projectName + File.separator + lang + ".json");
            String json = new GsonBuilder().setPrettyPrinting().create().toJson(model);
            SaveThread.getInstance().save(projectName, lang, langFile, json);
        }
    }



    /**
     * 移除翻译
     * @param key 翻译原文
     */
    @JsonIgnore
    public void remove(String key){
        messages.remove(key);
        if(extras != null){
            extras.remove(key);
        }
        save();
    }



    /**
     * 添加翻译
     *
     * @param key    翻译原文
     * @param value  翻译译文
     * @param common
     */
    public void put(String key, String value, String common){
        messages.put(key, value);
        if(common != null){
            if(extras == null){
                extras = new ConcurrentHashMap<>();
            }
            Extra ex = extras.computeIfAbsent(key, k -> {
                return new Extra();
            });
            ex.setCommon(common);
        }
        save();
    }



    /**
     * 添加翻译
     * @param key 翻译原文
     */
    public boolean contains(String key){
        return messages.containsKey(key);
    }

    public String getProjectName() {
        return projectName;
    }

    public void setProjectName(String projectName) {
        this.projectName = projectName;
    }


    /**
     * 生成对象
     */
    public KeywordFileModel getModel() {
        KeywordFileModel model = new KeywordFileModel();
        model.setLang(lang);
        model.getMessages().putAll(messages);
        model.setProjectName(projectName);
        model.setTitle(title);
        model.setExtras(extras);
        return model;
    }
}
